package com.nbx.app.zeuscmdport;

import java.nio.FloatBuffer;

import javax.microedition.khronos.egl.EGLConfig;
import javax.microedition.khronos.opengles.GL10;

import android.content.Context;
import android.opengl.GLU;
import android.view.MotionEvent;

/**
 * http://www.zeuscmd.com/tutorials/opengles/14-BackfaceCulling.php
 * 
 *
 */
public class Render14 extends RenderBase {
	private float xrot = 0.0f;
	private float yrot = 0.0f;

	private int mHeight = 0;
	
	private boolean acw = true;
	private boolean cullface = true;

	private float box[] = {
		// FRONT
		-0.5f, -0.5f,  0.5f,
		 0.5f, -0.5f,  0.5f,
		-0.5f,  0.5f,  0.5f,
		 0.5f,  0.5f,  0.5f,
		// BACK
		-0.5f, -0.5f, -0.5f,
		-0.5f,  0.5f, -0.5f,
		 0.5f, -0.5f, -0.5f,
		 0.5f,  0.5f, -0.5f,
		// LEFT
		-0.5f, -0.5f,  0.5f,
		-0.5f,  0.5f,  0.5f,
		-0.5f, -0.5f, -0.5f,
		-0.5f,  0.5f, -0.5f,
		// RIGHT
		 0.5f, -0.5f, -0.5f,
		 0.5f,  0.5f, -0.5f,
		 0.5f, -0.5f,  0.5f,
		 0.5f,  0.5f,  0.5f,
		// BOTTOM
		-0.5f, -0.5f,  0.5f,
		-0.5f, -0.5f, -0.5f,
		 0.5f, -0.5f,  0.5f,
		 0.5f, -0.5f, -0.5f,
	};


	
	private FloatBuffer mBoxBuffer;
	
	public Render14(Context context) {
		super(context);
	}

	public void onSurfaceCreated(GL10 gl, EGLConfig config) {
		
		mBoxBuffer = this.createFloatBuffer(box);
		
		gl.glEnable(GL10.GL_DEPTH_TEST);
		gl.glDepthFunc(GL10.GL_LEQUAL);

		gl.glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
		gl.glClearDepthf(1.0f);

		gl.glVertexPointer(3, GL10.GL_FLOAT, 0, mBoxBuffer);
		gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);

		gl.glShadeModel(GL10.GL_FLAT);

		gl.glEnable(GL10.GL_CULL_FACE);
	}

	public void onSurfaceChanged(GL10 gl, int width, int height) {
		mHeight = height;
		
		gl.glMatrixMode(GL10.GL_PROJECTION);
		gl.glLoadIdentity();

		gl.glViewport(0, 0, width, height);
		GLU.gluPerspective(gl, 45.0f, 1.0f * width / height, 1.0f, 100.0f);

		gl.glMatrixMode(GL10.GL_MODELVIEW);
		gl.glLoadIdentity();
	}

	public void onDrawFrame(GL10 gl) {
		gl.glClear(GL10.GL_COLOR_BUFFER_BIT | GL10.GL_DEPTH_BUFFER_BIT);
		gl.glLoadIdentity();

		if (acw) {
			gl.glFrontFace(GL10.GL_CCW);
		} else {
			gl.glFrontFace(GL10.GL_CW);
		}

		if (cullface) {
			gl.glEnable(GL10.GL_CULL_FACE);
		} else {
			gl.glDisable(GL10.GL_CULL_FACE);
		}

		GLU.gluLookAt(gl, 0.0f, 0.0f, 3.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f);

		gl.glRotatef(xrot, 1.0f, 0.0f, 0.0f);
		gl.glRotatef(yrot, 0.0f, 1.0f, 0.0f);

		gl.glColor4f(1.0f, 0.0f, 0.0f, 1.0f);
		gl.glDrawArrays(GL10.GL_TRIANGLE_STRIP, 0, 4);
		gl.glDrawArrays(GL10.GL_TRIANGLE_STRIP, 4, 4);

		gl.glColor4f(0.0f, 1.0f, 0.0f, 1.0f);
		gl.glDrawArrays(GL10.GL_TRIANGLE_STRIP, 8, 4);
		gl.glDrawArrays(GL10.GL_TRIANGLE_STRIP, 12, 4);

		gl.glColor4f(0.0f, 0.0f, 1.0f, 1.0f);
		gl.glDrawArrays(GL10.GL_TRIANGLE_STRIP, 16, 4);

		xrot += 2.0f;
		yrot += 3.0f;
	}

	@Override
	public boolean handleTouchEvent(MotionEvent event) {
		if (event.getAction() == MotionEvent.ACTION_UP) {
			final int y = (int)event.getY();
			
			if(y <= mHeight/2){
				acw = !acw;
			}else{
				cullface = !cullface;
			}
			return true;
		}
		return false;
	}	
}